package com.umeng.analytics.pro;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import android.util.Base64;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class w {
    public enum w$a {
        AUTOPAGE,
        PAGE,
        BEGIN,
        END,
        NEWSESSION
    }


    static class w$b {
        private static final w a = new w();

        private w$b() {

        }

        static w a() {
            return a;
        }
    }

    public static final int a = 2049;
    public static final int b = 2050;
    private static Context c = null;
    private static String d = null;
    private static final String e = "umeng+";
    private static final String f = "ek__id";
    private static final String g = "ek_key";
    private List h;

    static {
        c = null;
        d = null;
    }

    private w() {
        this.h = new ArrayList();
        if (w.c != null) {
            this.b();
            this.h.clear();
        }
    }

    public boolean a(String arg6, String arg7, int arg8) {
        Throwable v0_1;
        SQLiteDatabase v1_1 = null;
        SQLiteDatabase v0;
        try {
            v0 = u.a(w.c).a();
            v0.beginTransaction();
            ContentValues v1_2 = new ContentValues();
            v1_2.put("__i", arg6);
            String v2 = this.a(arg7);
            if (!TextUtils.isEmpty(((CharSequence) v2))) {
                v1_2.put("__a", v2);
                v1_2.put("__t", Integer.valueOf(arg8));
                v0.insert("__er", null, v1_2);
            }
            v0.setTransactionSuccessful();
        } catch (Exception e) {

        } finally {
            v1_1.endTransaction();
            u.a(w.c).b();
        }
        return false;
    }

    public static final w a(Context arg1) {
        w.c = arg1;
        return w$b.a();
    }

    public boolean a(String arg9, JSONObject arg10, w$a arg11) {
        String v1_3 = null;
        SQLiteDatabase v1_1;
        JSONObject v4_1;
        Cursor v0_1 = null;
        SQLiteDatabase database = null;
        Cursor v1 = null;
        if (arg10 == null) {
            return false;
        }

        try {
            database = u.a(w.c).a();
            database.beginTransaction();
            if (arg11 == w$a.BEGIN) {
                long v4 = ((Long) arg10.get("__e")).longValue();
                ContentValues v0_2 = new ContentValues();
                v0_2.put("__ii", arg9);
                v0_2.put("__e", String.valueOf(v4));
                database.insert("__sd", null, v0_2);
                v0_1 = v1;
                database.setTransactionSuccessful();
            } else if (arg11 == w$a.END) {
                database.execSQL("update __sd set __f=\"" + ((Long) arg10.get("__f")).longValue() + "\" where __ii=\"" + arg9 + "\"");
                v0_1 = v1;
                database.setTransactionSuccessful();
            } else if (arg11 == w$a.PAGE) {
                this.a(arg9, arg10, database, "__a");
                v0_1 = v1;
                database.setTransactionSuccessful();
            } else if (arg11 == w$a.AUTOPAGE) {
                this.a(arg9, arg10, database, "__b");
                v0_1 = v1;
                database.setTransactionSuccessful();
            } else if (arg11 == w$a.NEWSESSION) {
                v4_1 = arg10.getJSONObject("__d");
                String v3_1;
                if (v4_1 != null) {
                    v0_1 = database.rawQuery("select __d from __sd where __ii=\"" + arg9 + "\"", null);
                    if (v0_1 == null) {
                        v3_1 = null;
                    } else {
                        while (v0_1.moveToNext()) {
                            v1_3 = this.b(v0_1.getString(v0_1.getColumnIndex("__d")));
                        }
                        v3_1 = v1_3;
                    }
                } else {
                    v3_1 = null;
                    v0_1 = v1;
                }

                if (v4_1 == null) {

                } else {
                    JSONArray v1_5 = new JSONArray();
                    if (!TextUtils.isEmpty(v3_1)) {
                        v1_5 = new JSONArray(((String) v3_1));
                    }
                    v1_5.put(v4_1);
                    v1_3 = this.a(v1_5.toString());
                    if (TextUtils.isEmpty(v1_3)) {

                    } else {
                        database.execSQL("update  __sd set __d=\"" + v1_3 + "\" where __ii=\"" + arg9 + "\"");
                    }
                }
                JSONObject v1_6 = arg10.getJSONObject("__c");
                if (v1_6 == null) {

                } else {
                    v1_3 = this.a(v1_6.toString());
                    if (TextUtils.isEmpty(v1_3)) {

                    } else {
                        database.execSQL("update  __sd set __c=\"" + v1_3 + "\" where __ii=\"" + arg9 + "\"");
                    }
                }
                database.execSQL("update  __sd set __f=\"" + arg10.getLong("__f") + "\" where __ii=\"" + arg9 + "\"");
                database.setTransactionSuccessful();
            }
        } catch (Throwable v0) {

        } finally {
            if (v0_1 != null) {
                try {
                    v0_1.close();
                } catch (Exception e) {

                }
            }
            if(database != null) {
                database.endTransaction();
            }
            u.a(w.c).b();
        }
        return false;
    }

    public void a(JSONArray arg8) {
        int v2;
        Throwable v0_1;
        SQLiteDatabase v1_1;
        Throwable v6;
        SQLiteDatabase v0 = null;
        try {
            v0 = u.a(w.c).a();
            v0.beginTransaction();
            v2 = 0;
            while (true) {
                if (v2 >= arg8.length()) {
                    break;
                }
                try {
                    JSONObject v3 = arg8.getJSONObject(v2);
                    ContentValues v4 = new ContentValues();
                    String v1_3 = v3.optString("__i");
                    if (TextUtils.isEmpty(((CharSequence) v1_3))) {
                        v1_3 = bd.g(w.c);
                        if (TextUtils.isEmpty(((CharSequence) v1_3))) {
                            v1_3 = "";
                        }
                    }
                    v4.put("__i", v1_3);
                    v4.put("__e", v3.optString("id"));
                    v4.put("__t", Integer.valueOf(v3.optInt("__t")));
                    v3.remove("__i");
                    v3.remove("__t");
                    v4.put("__s", this.a(v3.toString()));
                    v0.insert("__et", null, v4);
                } catch (Exception v1_2) {
                }
                ++v2;
                continue;
            }
            v0.setTransactionSuccessful();
        } catch (Throwable v1) {

        } finally {
            v0.endTransaction();
            u.a(w.c).b();
            return;
        }
    }

    public JSONObject a() {
        JSONObject v0 = new JSONObject();
        JSONObject v1 = new JSONObject();
        this.c(v1);
        this.b(v1);
        this.a(v1);
        try {
            if (v1.length() <= 0) {
                return v0;
            }
            v0.put("body", v1);
        } catch (Throwable v1_1) {
        }

        return v0;
    }

    public void a(boolean arg6, boolean arg7) {
        SQLiteDatabase v1_1;
        SQLiteDatabase v0 = null;
        try {
            v1_1 = u.a(w.c).a();
            v1_1.beginTransaction();
            v1_1.execSQL("delete from __er");
            v1_1.execSQL("delete from __et");
            if (!arg7) {
                if (this.h.size() > 0) {
                    int v2;
                    for (v2 = 0; v2 < this.h.size(); ++v2) {
                        v1_1.execSQL("delete from __sd where __ii=\"" + this.h.get(v2) + "\"");
                    }
                }
                this.h.clear();
            } else if (arg6) {
                v1_1.execSQL("delete from __sd");
            }
            v1_1.setTransactionSuccessful();
        } catch (Throwable v0_1) {
        } finally {
            v0.endTransaction();
            u.a(w.c).b();
            return;
        }
    }

    private void a(String arg6, JSONObject arg7, SQLiteDatabase arg8, String arg9) throws JSONException {
        Cursor v0_1 = null;
        String v1 = null;
        String v2_1;
        try {
            v0_1 = arg8.rawQuery("select " + arg9 + " from __sd where __ii=\"" + arg6 + "\"", null);
            if (v0_1 == null) {
                v2_1 = v1;
            } else {
                while (v0_1.moveToNext()) {
                    v1 = this.b(v0_1.getString(v0_1.getColumnIndex(arg9)));
                }
                v2_1 = v1;
            }
            JSONArray v1_2 = new JSONArray();
            if (!TextUtils.isEmpty(v2_1)) {
                v1_2 = new JSONArray(((String) v2_1));
            }
            v1_2.put(arg7);
            v1 = this.a(v1_2.toString());
            if (!TextUtils.isEmpty(((CharSequence) v1))) {
                arg8.execSQL("update __sd set " + arg9 + "=\"" + v1 + "\" where __ii=\"" + arg6 + "\"");
            }
        } catch (Throwable v1_1) {

        } finally {
            if (v0_1 != null) {
                try {
                    v0_1.close();
                } catch (Exception e) {

                }
            }
        }
    }

    public String a(String arg4) {
        String v0 = null;
        try {
            if (TextUtils.isEmpty(w.d)) {
                return arg4;
            }

            arg4 = Base64.encodeToString(bt.a(arg4.getBytes(), w.d.getBytes()), 0);
        } catch (Exception v1) {
            arg4 = v0;
        }

        return arg4;
    }

    private void a(JSONObject arg12) {
        Object v0_4;
        JSONArray v3_1;
        String v3;
        String v7;
        String v0_2;
        JSONObject v5;
        JSONObject v4;
        SQLiteDatabase v1_1;
        Cursor v1_2 = null;
        Throwable v0_1;
        SQLiteDatabase v2 = null;
        Cursor v0 = null;
        try {
            v2 = u.a(w.c).a();
            v2.beginTransaction();
            v1_2 = v2.rawQuery("select *  from __et", null);
            if (v1_2 != null) {
                JSONObject v6_1;
                v4 = new JSONObject();
                v5 = new JSONObject();
                while (true) {
                    if (v1_2.moveToNext()) {
                        int v6 = v1_2.getInt(v1_2.getColumnIndex("__t"));
                        v0_2 = v1_2.getString(v1_2.getColumnIndex("__i"));
                        v7 = v1_2.getString(v1_2.getColumnIndex("__s"));
                        if ("".equals(v0_2)) {
                            v3 = bd.a();
                        } else {
                            v3 = v0_2;
                        }
                        switch (v6) {
                            case 2049: {
                                if (TextUtils.isEmpty(((CharSequence) v7))) {

                                } else {
                                    v6_1 = new JSONObject(this.b(v7));
                                    JSONArray v0_3 = v4.has(v3) ? v4.optJSONArray(v3) : new JSONArray();
                                    v0_3.put(v6_1);
                                    v4.put(v3, v0_3);
                                }
                                break;
                            }
                            case 2050: {
                                if (TextUtils.isEmpty(((CharSequence) v7))) {

                                } else {
                                    v6_1 = new JSONObject(this.b(v7));
                                    JSONArray v0_3 = v5.has(v3) ? v5.optJSONArray(v3) : new JSONArray();
                                    v0_3.put(v6_1);
                                    v5.put(v3, v0_3);
                                }
                                break;
                            }
                        }
                        continue;
                    } else {
                        if (v4.length() > 0) {
                            v3_1 = new JSONArray();
                            Iterator v6_2 = v4.keys();
                            while (v6_2.hasNext()) {
                                JSONObject v7_1 = new JSONObject();
                                v0_4 = v6_2.next();
                                v7_1.put(((String) v0_4), new JSONArray(v4.optString(((String) v0_4))));
                                if (v7_1.length() <= 0) {
                                    continue;
                                }
                                v3_1.put(v7_1);
                            }
                            if (v3_1.length() > 0) {
                                arg12.put("ekv", v3_1);
                            }
                        }

                        if (v5.length() > 0) {
                            v3_1 = new JSONArray();
                            Iterator v4_1 = v5.keys();
                            while (v4_1.hasNext()) {
                                v6_1 = new JSONObject();
                                v0_4 = v4_1.next();
                                v6_1.put(((String) v0_4), new JSONArray(v5.optString(((String) v0_4))));
                                if (v6_1.length() <= 0) {
                                    continue;
                                }

                                v3_1.put(v6_1);
                            }

                            if (v3_1.length() <= 0) {
                            } else {
                                arg12.put("gkv", v3_1);
                            }
                        }
                        v2.setTransactionSuccessful();
                    }
                }
            }
        } catch (Throwable v1) {

        } finally {
            if (v1_2 != null) {
                try {
                    v1_2.close();
                } catch (Exception e) {

                }
            }
            v2.endTransaction();
            u.a(w.c).b();
        }
    }

    private void b() {
        int v0 = 0;
        try {
            if (TextUtils.isEmpty(w.d)) {
                SharedPreferences v2 = ba.a(w.c);
                String v1 = v2.getString("ek__id", null);
                if (TextUtils.isEmpty(((CharSequence) v1))) {
                    v1 = bv.A(w.c);
                    if (!TextUtils.isEmpty(((CharSequence) v1))) {
                        v2.edit().putString("ek__id", v1).commit();
                    }
                }

                if (!TextUtils.isEmpty(((CharSequence) v1))) {
                    v1 = v1.substring(1, 9);
                    StringBuilder v3 = new StringBuilder();
                    while (v0 < v1.length()) {
                        char v4 = v1.charAt(v0);
                        if (!Character.isDigit(v4)) {
                            v3.append(v4);
                        } else if (Integer.parseInt(Character.toString(v4)) == 0) {
                            v3.append(0);
                        } else {
                            v3.append(10 - Integer.parseInt(Character.toString(v4)));
                        }

                        ++v0;
                    }

                    w.d = v3.toString();
                }

                if (TextUtils.isEmpty(w.d)) {
                    return;
                }

                w.d = w.d + new StringBuilder(w.d).reverse().toString();
                String v0_2 = v2.getString("ek_key", null);
                if (TextUtils.isEmpty(((CharSequence) v0_2))) {
                    v2.edit().putString("ek_key", this.a("umeng+")).commit();
                    return;
                }

                if (!"umeng+".equals(this.b(v0_2))) {
                    this.a(true, false);
                }
            }

            return;
        } catch (Throwable v0_1) {
            return;
        }
    }

    public String b(String arg4) {
        String v0 = null;
        try {
            if (TextUtils.isEmpty(w.d)) {
                return arg4;
            }

            arg4 = new String(bt.b(Base64.decode(arg4.getBytes(), 0), w.d.getBytes()));
        } catch (Exception v1) {
            arg4 = v0;
        }

        return arg4;
    }

    private void b(JSONObject arg7) {
        JSONArray v2_2;
        Throwable v5_1;
        Cursor v1_2;
        Throwable v0_1;
        SQLiteDatabase v2;
        SQLiteDatabase v1_1 = null;
        Cursor v0 = null;
        try {
            v1_1 = u.a(w.c).a();
            v1_1.beginTransaction();
            v0 = v1_1.rawQuery("select *  from __er", null);
            if (v0 != null) {
                v2_2 = new JSONArray();
                while (v0.moveToNext()) {
                    String v3 = v0.getString(v0.getColumnIndex("__a"));
                    if (TextUtils.isEmpty(((CharSequence) v3))) {
                        continue;
                    }

                    v2_2.put(new JSONObject(this.b(v3)));
                }
                if (v2_2.length() > 0) {
                    arg7.put("error", v2_2);
                }
            }
            v1_1.setTransactionSuccessful();
        } catch (Throwable v2_1) {
            label_62:
            v5_1 = v2_1;
            v2 = v1_1;
            v1_2 = v0;
            v0_1 = v5_1;
        } finally {
            if (v0 != null) {
                try {
                    v0.close();
                } catch (Exception e) {

                }
            }

            v1_1.endTransaction();
            u.a(w.c).b();
            return;
        }
    }

    private void c(JSONObject arg18) {
        JSONArray v4_2;
        Throwable v2_1;
        Cursor v3_1;
        SQLiteDatabase v4_1;
        Throwable v16;
        SQLiteDatabase v3 = null;
        Cursor v2 = null;
        try {
            v3 = u.a(w.c).a();
            v3.beginTransaction();
            v2 = v3.rawQuery("select *  from __sd", null);
            if (v2 == null) {
            } else {
                v4_2 = new JSONArray();
                this.h.clear();
                while (v2.moveToNext()) {
                    JSONObject v5 = new JSONObject();
                    String v6 = v2.getString(v2.getColumnIndex("__f"));
                    String v7 = v2.getString(v2.getColumnIndex("__e"));
                    if (TextUtils.isEmpty(((CharSequence) v6))) {
                        continue;
                    }

                    if (TextUtils.isEmpty(((CharSequence) v7))) {
                        continue;
                    }

                    if (Long.parseLong(v6) - Long.parseLong(v7) <= 0) {
                        continue;
                    }

                    String v8 = v2.getString(v2.getColumnIndex("__a"));
                    String v9 = v2.getString(v2.getColumnIndex("__b"));
                    String v10 = v2.getString(v2.getColumnIndex("__c"));
                    String v11 = v2.getString(v2.getColumnIndex("__d"));
                    String v12 = v2.getString(v2.getColumnIndex("__ii"));
                    this.h.add(v12);
                    v5.put("id", v12);
                    v5.put("start_time", v7);
                    v5.put("end_time", v6);
                    v5.put("duration", Long.parseLong(v6) - Long.parseLong(v7));
                    if (!TextUtils.isEmpty(((CharSequence) v8))) {
                        v5.put("pages", new JSONArray(this.b(v8)));
                    }

                    if (!TextUtils.isEmpty(((CharSequence) v9))) {
                        v5.put("autopages", new JSONArray(this.b(v9)));
                    }

                    if (!TextUtils.isEmpty(((CharSequence) v10))) {
                        v5.put("traffic", new JSONObject(this.b(v10)));
                    }

                    if (!TextUtils.isEmpty(((CharSequence) v11))) {
                        v5.put("locations", new JSONArray(this.b(v11)));
                    }

                    if (v5.length() <= 0) {
                        continue;
                    }

                    v4_2.put(v5);
                }
                if (v4_2.length() > 0) {
                    arg18.put("sessions", v4_2);
                }
            }
            v3.setTransactionSuccessful();
        } catch (Throwable v4) {

        } finally {
            if (v2 != null) {
                try {
                    v2.close();
                } catch (Exception e) {

                }
            }
            v3.endTransaction();
            u.a(w.c).b();
            return;
        }
    }
}

